package cn.yuemouren.leetcode;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: Timi
 * @Description: 利用区间解决最长不重复子串问题
 * @Date: 2020/7/21 9:25
 */
public class Test_3 {

    public int lengthOfLongestSubstring(String s) {
        //"abcabcbb"
        int length = s.length(),res = 0;
        Map<Character,Integer> map = new HashMap<>();
        for (int i = 0,j = 0; j < length; j++){
            if(map.containsKey(s.charAt(j))){
                i = Math.max(map.get(s.charAt(j)),i);
            }
            res = Math.max(res,j - i + 1);
            map.put(s.charAt(j),j + 1);
        }
        return res;
    }
}
